<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    
    <title>16.6. logging — Logging facility for Python &mdash; Python 3.4.3 documentation</title>
    
    <link rel="stylesheet" href="../_static/pydoctheme.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../',
        VERSION:     '3.4.3',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 3.4.3 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="top" title="Python 3.4.3 documentation" href="../index.html" />
    <link rel="up" title="16. Generic Operating System Services" href="allos.html" />
    <link rel="next" title="16.7. logging.config — Logging configuration" href="logging.config.html" />
    <link rel="prev" title="16.5. getopt — C-style parser for command line options" href="getopt.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    <script type="text/javascript" src="../_static/version_switch.js"></script>
    
 

  </head>
  <body>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="logging.config.html" title="16.7. logging.config — Logging configuration"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="getopt.html" title="16.5. getopt — C-style parser for command line options"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.4.3</span>
          <a href="../index.html">Documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="allos.html" accesskey="U">16. Generic Operating System Services</a> &raquo;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">
            
  <div class="section" id="module-logging">
<span id="logging-logging-facility-for-python"></span><h1>16.6. <a class="reference internal" href="#module-logging" title="logging: Flexible event logging system for applications."><tt class="xref py py-mod docutils literal"><span class="pre">logging</span></tt></a> &#8212; Logging facility for Python<a class="headerlink" href="#module-logging" title="Permalink to this headline">¶</a></h1>
<div class="sidebar" id="index-0">
<p class="first sidebar-title">Important</p>
<p>This page contains the API reference information. For tutorial
information and discussion of more advanced topics, see</p>
<ul class="last simple">
<li><a class="reference internal" href="../howto/logging.html#logging-basic-tutorial"><em>Basic Tutorial</em></a></li>
<li><a class="reference internal" href="../howto/logging.html#logging-advanced-tutorial"><em>Advanced Tutorial</em></a></li>
<li><a class="reference internal" href="../howto/logging-cookbook.html#logging-cookbook"><em>Logging Cookbook</em></a></li>
</ul>
</div>
<p><strong>Source code:</strong> <a class="reference external" href="https://hg.python.org/cpython/file/3.4/Lib/logging/__init__.py">Lib/logging/__init__.py</a></p>
<hr class="docutils" />
<p>This module defines functions and classes which implement a flexible event
logging system for applications and libraries.</p>
<p>The key benefit of having the logging API provided by a standard library module
is that all Python modules can participate in logging, so your application log
can include your own messages integrated with messages from third-party
modules.</p>
<p>The module provides a lot of functionality and flexibility.  If you are
unfamiliar with logging, the best way to get to grips with it is to see the
tutorials (see the links on the right).</p>
<p>The basic classes defined by the module, together with their functions, are
listed below.</p>
<ul class="simple">
<li>Loggers expose the interface that application code directly uses.</li>
<li>Handlers send the log records (created by loggers) to the appropriate
destination.</li>
<li>Filters provide a finer grained facility for determining which log records
to output.</li>
<li>Formatters specify the layout of log records in the final output.</li>
</ul>
<div class="section" id="logger-objects">
<span id="logger"></span><h2>16.6.1. Logger Objects<a class="headerlink" href="#logger-objects" title="Permalink to this headline">¶</a></h2>
<p>Loggers have the following attributes and methods.  Note that Loggers are never
instantiated directly, but always through the module-level function
<tt class="docutils literal"><span class="pre">logging.getLogger(name)</span></tt>.  Multiple calls to <a class="reference internal" href="#logging.getLogger" title="logging.getLogger"><tt class="xref py py-func docutils literal"><span class="pre">getLogger()</span></tt></a> with the same
name will always return a reference to the same Logger object.</p>
<p>The <tt class="docutils literal"><span class="pre">name</span></tt> is potentially a period-separated hierarchical value, like
<tt class="docutils literal"><span class="pre">foo.bar.baz</span></tt> (though it could also be just plain <tt class="docutils literal"><span class="pre">foo</span></tt>, for example).
Loggers that are further down in the hierarchical list are children of loggers
higher up in the list.  For example, given a logger with a name of <tt class="docutils literal"><span class="pre">foo</span></tt>,
loggers with names of <tt class="docutils literal"><span class="pre">foo.bar</span></tt>, <tt class="docutils literal"><span class="pre">foo.bar.baz</span></tt>, and <tt class="docutils literal"><span class="pre">foo.bam</span></tt> are all
descendants of <tt class="docutils literal"><span class="pre">foo</span></tt>.  The logger name hierarchy is analogous to the Python
package hierarchy, and identical to it if you organise your loggers on a
per-module basis using the recommended construction
<tt class="docutils literal"><span class="pre">logging.getLogger(__name__)</span></tt>.  That&#8217;s because in a module, <tt class="docutils literal"><span class="pre">__name__</span></tt>
is the module&#8217;s name in the Python package namespace.</p>
<dl class="class">
<dt id="logging.Logger">
<em class="property">class </em><tt class="descclassname">logging.</tt><tt class="descname">Logger</tt><a class="headerlink" href="#logging.Logger" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<dl class="attribute">
<dt id="logging.Logger.propagate">
<tt class="descclassname">Logger.</tt><tt class="descname">propagate</tt><a class="headerlink" href="#logging.Logger.propagate" title="Permalink to this definition">¶</a></dt>
<dd><p>If this evaluates to true, events logged to this logger will be passed to the
handlers of higher level (ancestor) loggers, in addition to any handlers
attached to this logger. Messages are passed directly to the ancestor
loggers&#8217; handlers - neither the level nor filters of the ancestor loggers in
question are considered.</p>
<p>If this evaluates to false, logging messages are not passed to the handlers
of ancestor loggers.</p>
<p>The constructor sets this attribute to <tt class="docutils literal"><span class="pre">True</span></tt>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you attach a handler to a logger <em>and</em> one or more of its
ancestors, it may emit the same record multiple times. In general, you
should not need to attach a handler to more than one logger - if you just
attach it to the appropriate logger which is highest in the logger
hierarchy, then it will see all events logged by all descendant loggers,
provided that their propagate setting is left set to <tt class="docutils literal"><span class="pre">True</span></tt>. A common
scenario is to attach handlers only to the root logger, and to let
propagation take care of the rest.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.setLevel">
<tt class="descclassname">Logger.</tt><tt class="descname">setLevel</tt><big>(</big><em>lvl</em><big>)</big><a class="headerlink" href="#logging.Logger.setLevel" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets the threshold for this logger to <em>lvl</em>. Logging messages which are less
severe than <em>lvl</em> will be ignored. When a logger is created, the level is set to
<tt class="xref py py-const docutils literal"><span class="pre">NOTSET</span></tt> (which causes all messages to be processed when the logger is
the root logger, or delegation to the parent when the logger is a non-root
logger). Note that the root logger is created with level <tt class="xref py py-const docutils literal"><span class="pre">WARNING</span></tt>.</p>
<p>The term &#8216;delegation to the parent&#8217; means that if a logger has a level of
NOTSET, its chain of ancestor loggers is traversed until either an ancestor with
a level other than NOTSET is found, or the root is reached.</p>
<p>If an ancestor is found with a level other than NOTSET, then that ancestor&#8217;s
level is treated as the effective level of the logger where the ancestor search
began, and is used to determine how a logging event is handled.</p>
<p>If the root is reached, and it has a level of NOTSET, then all messages will be
processed. Otherwise, the root&#8217;s level will be used as the effective level.</p>
<p>See <a class="reference internal" href="#levels"><em>Logging Levels</em></a> for a list of levels.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>The <em>lvl</em> parameter now accepts a string representation of the
level such as &#8216;INFO&#8217; as an alternative to the integer constants
such as <tt class="xref py py-const docutils literal"><span class="pre">INFO</span></tt>. Note, however, that levels are internally stored
as integers, and methods such as e.g. <a class="reference internal" href="#logging.Logger.getEffectiveLevel" title="logging.Logger.getEffectiveLevel"><tt class="xref py py-meth docutils literal"><span class="pre">getEffectiveLevel()</span></tt></a> and
<a class="reference internal" href="#logging.Logger.isEnabledFor" title="logging.Logger.isEnabledFor"><tt class="xref py py-meth docutils literal"><span class="pre">isEnabledFor()</span></tt></a> will return/expect to be passed integers.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.isEnabledFor">
<tt class="descclassname">Logger.</tt><tt class="descname">isEnabledFor</tt><big>(</big><em>lvl</em><big>)</big><a class="headerlink" href="#logging.Logger.isEnabledFor" title="Permalink to this definition">¶</a></dt>
<dd><p>Indicates if a message of severity <em>lvl</em> would be processed by this logger.
This method checks first the module-level level set by
<tt class="docutils literal"><span class="pre">logging.disable(lvl)</span></tt> and then the logger&#8217;s effective level as determined
by <a class="reference internal" href="#logging.Logger.getEffectiveLevel" title="logging.Logger.getEffectiveLevel"><tt class="xref py py-meth docutils literal"><span class="pre">getEffectiveLevel()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.getEffectiveLevel">
<tt class="descclassname">Logger.</tt><tt class="descname">getEffectiveLevel</tt><big>(</big><big>)</big><a class="headerlink" href="#logging.Logger.getEffectiveLevel" title="Permalink to this definition">¶</a></dt>
<dd><p>Indicates the effective level for this logger. If a value other than
<tt class="xref py py-const docutils literal"><span class="pre">NOTSET</span></tt> has been set using <a class="reference internal" href="#logging.Logger.setLevel" title="logging.Logger.setLevel"><tt class="xref py py-meth docutils literal"><span class="pre">setLevel()</span></tt></a>, it is returned. Otherwise,
the hierarchy is traversed towards the root until a value other than
<tt class="xref py py-const docutils literal"><span class="pre">NOTSET</span></tt> is found, and that value is returned. The value returned is
an integer, typically one of <tt class="xref py py-const docutils literal"><span class="pre">logging.DEBUG</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">logging.INFO</span></tt>
etc.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.getChild">
<tt class="descclassname">Logger.</tt><tt class="descname">getChild</tt><big>(</big><em>suffix</em><big>)</big><a class="headerlink" href="#logging.Logger.getChild" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a logger which is a descendant to this logger, as determined by the suffix.
Thus, <tt class="docutils literal"><span class="pre">logging.getLogger('abc').getChild('def.ghi')</span></tt> would return the same
logger as would be returned by <tt class="docutils literal"><span class="pre">logging.getLogger('abc.def.ghi')</span></tt>. This is a
convenience method, useful when the parent logger is named using e.g. <tt class="docutils literal"><span class="pre">__name__</span></tt>
rather than a literal string.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2.</span></p>
</div>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.debug">
<tt class="descclassname">Logger.</tt><tt class="descname">debug</tt><big>(</big><em>msg</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#logging.Logger.debug" title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <tt class="xref py py-const docutils literal"><span class="pre">DEBUG</span></tt> on this logger. The <em>msg</em> is the
message format string, and the <em>args</em> are the arguments which are merged into
<em>msg</em> using the string formatting operator. (Note that this means that you can
use keywords in the format string, together with a single dictionary argument.)</p>
<p>There are three keyword arguments in <em>kwargs</em> which are inspected: <em>exc_info</em>
which, if it does not evaluate as false, causes exception information to be
added to the logging message. If an exception tuple (in the format returned by
<a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><tt class="xref py py-func docutils literal"><span class="pre">sys.exc_info()</span></tt></a>) is provided, it is used; otherwise, <a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><tt class="xref py py-func docutils literal"><span class="pre">sys.exc_info()</span></tt></a>
is called to get the exception information.</p>
<p>The second optional keyword argument is <em>stack_info</em>, which defaults to
<tt class="docutils literal"><span class="pre">False</span></tt>. If true, stack information is added to the logging
message, including the actual logging call. Note that this is not the same
stack information as that displayed through specifying <em>exc_info</em>: The
former is stack frames from the bottom of the stack up to the logging call
in the current thread, whereas the latter is information about stack frames
which have been unwound, following an exception, while searching for
exception handlers.</p>
<p>You can specify <em>stack_info</em> independently of <em>exc_info</em>, e.g. to just show
how you got to a certain point in your code, even when no exceptions were
raised. The stack frames are printed following a header line which says:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">Stack</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
</pre></div>
</div>
<p>This mimics the <tt class="docutils literal"><span class="pre">Traceback</span> <span class="pre">(most</span> <span class="pre">recent</span> <span class="pre">call</span> <span class="pre">last):</span></tt> which is used when
displaying exception frames.</p>
<p>The third keyword argument is <em>extra</em> which can be used to pass a
dictionary which is used to populate the __dict__ of the LogRecord created for
the logging event with user-defined attributes. These custom attributes can then
be used as you like. For example, they could be incorporated into logged
messages. For example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">FORMAT</span> <span class="o">=</span> <span class="s">&#39;%(asctime)-15s %(clientip)s %(user)-8s %(message)s&#39;</span>
<span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="nb">format</span><span class="o">=</span><span class="n">FORMAT</span><span class="p">)</span>
<span class="n">d</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;clientip&#39;</span><span class="p">:</span> <span class="s">&#39;192.168.0.1&#39;</span><span class="p">,</span> <span class="s">&#39;user&#39;</span><span class="p">:</span> <span class="s">&#39;fbloggs&#39;</span><span class="p">}</span>
<span class="n">logger</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogger</span><span class="p">(</span><span class="s">&#39;tcpserver&#39;</span><span class="p">)</span>
<span class="n">logger</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s">&#39;Protocol problem: %s&#39;</span><span class="p">,</span> <span class="s">&#39;connection reset&#39;</span><span class="p">,</span> <span class="n">extra</span><span class="o">=</span><span class="n">d</span><span class="p">)</span>
</pre></div>
</div>
<p>would print something like</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="mi">2006</span><span class="o">-</span><span class="mi">02</span><span class="o">-</span><span class="mi">08</span> <span class="mi">22</span><span class="p">:</span><span class="mi">20</span><span class="p">:</span><span class="mi">02</span><span class="p">,</span><span class="mi">165</span> <span class="mf">192.168</span><span class="o">.</span><span class="mf">0.1</span> <span class="n">fbloggs</span>  <span class="n">Protocol</span> <span class="n">problem</span><span class="p">:</span> <span class="n">connection</span> <span class="n">reset</span>
</pre></div>
</div>
<p>The keys in the dictionary passed in <em>extra</em> should not clash with the keys used
by the logging system. (See the <a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><tt class="xref py py-class docutils literal"><span class="pre">Formatter</span></tt></a> documentation for more
information on which keys are used by the logging system.)</p>
<p>If you choose to use these attributes in logged messages, you need to exercise
some care. In the above example, for instance, the <a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><tt class="xref py py-class docutils literal"><span class="pre">Formatter</span></tt></a> has been
set up with a format string which expects &#8216;clientip&#8217; and &#8216;user&#8217; in the attribute
dictionary of the LogRecord. If these are missing, the message will not be
logged because a string formatting exception will occur. So in this case, you
always need to pass the <em>extra</em> dictionary with these keys.</p>
<p>While this might be annoying, this feature is intended for use in specialized
circumstances, such as multi-threaded servers where the same code executes in
many contexts, and interesting conditions which arise are dependent on this
context (such as remote client IP address and authenticated user name, in the
above example). In such circumstances, it is likely that specialized
<a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><tt class="xref py py-class docutils literal"><span class="pre">Formatter</span></tt></a>s would be used with particular <tt class="xref py py-class docutils literal"><span class="pre">Handler</span></tt>s.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2: </span>The <em>stack_info</em> parameter was added.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.info">
<tt class="descclassname">Logger.</tt><tt class="descname">info</tt><big>(</big><em>msg</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#logging.Logger.info" title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <tt class="xref py py-const docutils literal"><span class="pre">INFO</span></tt> on this logger. The arguments are
interpreted as for <a class="reference internal" href="#logging.debug" title="logging.debug"><tt class="xref py py-meth docutils literal"><span class="pre">debug()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.warning">
<tt class="descclassname">Logger.</tt><tt class="descname">warning</tt><big>(</big><em>msg</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#logging.Logger.warning" title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <tt class="xref py py-const docutils literal"><span class="pre">WARNING</span></tt> on this logger. The arguments are
interpreted as for <a class="reference internal" href="#logging.debug" title="logging.debug"><tt class="xref py py-meth docutils literal"><span class="pre">debug()</span></tt></a>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">There is an obsolete method <tt class="docutils literal"><span class="pre">warn</span></tt> which is functionally
identical to <tt class="docutils literal"><span class="pre">warning</span></tt>. As <tt class="docutils literal"><span class="pre">warn</span></tt> is deprecated, please do not use
it - use <tt class="docutils literal"><span class="pre">warning</span></tt> instead.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.error">
<tt class="descclassname">Logger.</tt><tt class="descname">error</tt><big>(</big><em>msg</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#logging.Logger.error" title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <tt class="xref py py-const docutils literal"><span class="pre">ERROR</span></tt> on this logger. The arguments are
interpreted as for <a class="reference internal" href="#logging.debug" title="logging.debug"><tt class="xref py py-meth docutils literal"><span class="pre">debug()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.critical">
<tt class="descclassname">Logger.</tt><tt class="descname">critical</tt><big>(</big><em>msg</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#logging.Logger.critical" title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <tt class="xref py py-const docutils literal"><span class="pre">CRITICAL</span></tt> on this logger. The arguments are
interpreted as for <a class="reference internal" href="#logging.debug" title="logging.debug"><tt class="xref py py-meth docutils literal"><span class="pre">debug()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.log">
<tt class="descclassname">Logger.</tt><tt class="descname">log</tt><big>(</big><em>lvl</em>, <em>msg</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#logging.Logger.log" title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with integer level <em>lvl</em> on this logger. The other arguments are
interpreted as for <a class="reference internal" href="#logging.debug" title="logging.debug"><tt class="xref py py-meth docutils literal"><span class="pre">debug()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.exception">
<tt class="descclassname">Logger.</tt><tt class="descname">exception</tt><big>(</big><em>msg</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#logging.Logger.exception" title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <tt class="xref py py-const docutils literal"><span class="pre">ERROR</span></tt> on this logger. The arguments are
interpreted as for <a class="reference internal" href="#logging.debug" title="logging.debug"><tt class="xref py py-meth docutils literal"><span class="pre">debug()</span></tt></a>. Exception info is added to the logging
message. This method should only be called from an exception handler.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.addFilter">
<tt class="descclassname">Logger.</tt><tt class="descname">addFilter</tt><big>(</big><em>filt</em><big>)</big><a class="headerlink" href="#logging.Logger.addFilter" title="Permalink to this definition">¶</a></dt>
<dd><p>Adds the specified filter <em>filt</em> to this logger.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.removeFilter">
<tt class="descclassname">Logger.</tt><tt class="descname">removeFilter</tt><big>(</big><em>filt</em><big>)</big><a class="headerlink" href="#logging.Logger.removeFilter" title="Permalink to this definition">¶</a></dt>
<dd><p>Removes the specified filter <em>filt</em> from this logger.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.filter">
<tt class="descclassname">Logger.</tt><tt class="descname">filter</tt><big>(</big><em>record</em><big>)</big><a class="headerlink" href="#logging.Logger.filter" title="Permalink to this definition">¶</a></dt>
<dd><p>Applies this logger&#8217;s filters to the record and returns a true value if the
record is to be processed. The filters are consulted in turn, until one of
them returns a false value. If none of them return a false value, the record
will be processed (passed to handlers). If one returns a false value, no
further processing of the record occurs.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.addHandler">
<tt class="descclassname">Logger.</tt><tt class="descname">addHandler</tt><big>(</big><em>hdlr</em><big>)</big><a class="headerlink" href="#logging.Logger.addHandler" title="Permalink to this definition">¶</a></dt>
<dd><p>Adds the specified handler <em>hdlr</em> to this logger.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.removeHandler">
<tt class="descclassname">Logger.</tt><tt class="descname">removeHandler</tt><big>(</big><em>hdlr</em><big>)</big><a class="headerlink" href="#logging.Logger.removeHandler" title="Permalink to this definition">¶</a></dt>
<dd><p>Removes the specified handler <em>hdlr</em> from this logger.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.findCaller">
<tt class="descclassname">Logger.</tt><tt class="descname">findCaller</tt><big>(</big><em>stack_info=False</em><big>)</big><a class="headerlink" href="#logging.Logger.findCaller" title="Permalink to this definition">¶</a></dt>
<dd><p>Finds the caller&#8217;s source filename and line number. Returns the filename, line
number, function name and stack information as a 4-element tuple. The stack
information is returned as <em>None</em> unless <em>stack_info</em> is <em>True</em>.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.handle">
<tt class="descclassname">Logger.</tt><tt class="descname">handle</tt><big>(</big><em>record</em><big>)</big><a class="headerlink" href="#logging.Logger.handle" title="Permalink to this definition">¶</a></dt>
<dd><p>Handles a record by passing it to all handlers associated with this logger and
its ancestors (until a false value of <em>propagate</em> is found). This method is used
for unpickled records received from a socket, as well as those created locally.
Logger-level filtering is applied using <a class="reference internal" href="#logging.Logger.filter" title="logging.Logger.filter"><tt class="xref py py-meth docutils literal"><span class="pre">filter()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.makeRecord">
<tt class="descclassname">Logger.</tt><tt class="descname">makeRecord</tt><big>(</big><em>name</em>, <em>lvl</em>, <em>fn</em>, <em>lno</em>, <em>msg</em>, <em>args</em>, <em>exc_info</em>, <em>func=None</em>, <em>extra=None</em>, <em>sinfo=None</em><big>)</big><a class="headerlink" href="#logging.Logger.makeRecord" title="Permalink to this definition">¶</a></dt>
<dd><p>This is a factory method which can be overridden in subclasses to create
specialized <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><tt class="xref py py-class docutils literal"><span class="pre">LogRecord</span></tt></a> instances.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Logger.hasHandlers">
<tt class="descclassname">Logger.</tt><tt class="descname">hasHandlers</tt><big>(</big><big>)</big><a class="headerlink" href="#logging.Logger.hasHandlers" title="Permalink to this definition">¶</a></dt>
<dd><p>Checks to see if this logger has any handlers configured. This is done by
looking for handlers in this logger and its parents in the logger hierarchy.
Returns <tt class="docutils literal"><span class="pre">True</span></tt> if a handler was found, else <tt class="docutils literal"><span class="pre">False</span></tt>. The method stops searching
up the hierarchy whenever a logger with the &#8216;propagate&#8217; attribute set to
False is found - that will be the last logger which is checked for the
existence of handlers.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2.</span></p>
</div>
</dd></dl>

</div>
<div class="section" id="logging-levels">
<span id="levels"></span><h2>16.6.2. Logging Levels<a class="headerlink" href="#logging-levels" title="Permalink to this headline">¶</a></h2>
<p>The numeric values of logging levels are given in the following table. These are
primarily of interest if you want to define your own levels, and need them to
have specific values relative to the predefined levels. If you define a level
with the same numeric value, it overwrites the predefined value; the predefined
name is lost.</p>
<table border="1" class="docutils">
<colgroup>
<col width="48%" />
<col width="52%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Level</th>
<th class="head">Numeric value</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">CRITICAL</span></tt></td>
<td>50</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">ERROR</span></tt></td>
<td>40</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">WARNING</span></tt></td>
<td>30</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">INFO</span></tt></td>
<td>20</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">DEBUG</span></tt></td>
<td>10</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">NOTSET</span></tt></td>
<td>0</td>
</tr>
</tbody>
</table>
</div>
<div class="section" id="handler-objects">
<span id="handler"></span><h2>16.6.3. Handler Objects<a class="headerlink" href="#handler-objects" title="Permalink to this headline">¶</a></h2>
<p>Handlers have the following attributes and methods. Note that <tt class="xref py py-class docutils literal"><span class="pre">Handler</span></tt>
is never instantiated directly; this class acts as a base for more useful
subclasses. However, the <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a> method in subclasses needs to call
<a class="reference internal" href="#logging.Handler.__init__" title="logging.Handler.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">Handler.__init__()</span></tt></a>.</p>
<dl class="method">
<dt id="logging.Handler.__init__">
<tt class="descclassname">Handler.</tt><tt class="descname">__init__</tt><big>(</big><em>level=NOTSET</em><big>)</big><a class="headerlink" href="#logging.Handler.__init__" title="Permalink to this definition">¶</a></dt>
<dd><p>Initializes the <tt class="xref py py-class docutils literal"><span class="pre">Handler</span></tt> instance by setting its level, setting the list
of filters to the empty list and creating a lock (using <a class="reference internal" href="#logging.Handler.createLock" title="logging.Handler.createLock"><tt class="xref py py-meth docutils literal"><span class="pre">createLock()</span></tt></a>) for
serializing access to an I/O mechanism.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Handler.createLock">
<tt class="descclassname">Handler.</tt><tt class="descname">createLock</tt><big>(</big><big>)</big><a class="headerlink" href="#logging.Handler.createLock" title="Permalink to this definition">¶</a></dt>
<dd><p>Initializes a thread lock which can be used to serialize access to underlying
I/O functionality which may not be threadsafe.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Handler.acquire">
<tt class="descclassname">Handler.</tt><tt class="descname">acquire</tt><big>(</big><big>)</big><a class="headerlink" href="#logging.Handler.acquire" title="Permalink to this definition">¶</a></dt>
<dd><p>Acquires the thread lock created with <a class="reference internal" href="#logging.Handler.createLock" title="logging.Handler.createLock"><tt class="xref py py-meth docutils literal"><span class="pre">createLock()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Handler.release">
<tt class="descclassname">Handler.</tt><tt class="descname">release</tt><big>(</big><big>)</big><a class="headerlink" href="#logging.Handler.release" title="Permalink to this definition">¶</a></dt>
<dd><p>Releases the thread lock acquired with <a class="reference internal" href="#logging.Handler.acquire" title="logging.Handler.acquire"><tt class="xref py py-meth docutils literal"><span class="pre">acquire()</span></tt></a>.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Handler.setLevel">
<tt class="descclassname">Handler.</tt><tt class="descname">setLevel</tt><big>(</big><em>lvl</em><big>)</big><a class="headerlink" href="#logging.Handler.setLevel" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets the threshold for this handler to <em>lvl</em>. Logging messages which are less
severe than <em>lvl</em> will be ignored. When a handler is created, the level is set
to <tt class="xref py py-const docutils literal"><span class="pre">NOTSET</span></tt> (which causes all messages to be processed).</p>
<p>See <a class="reference internal" href="#levels"><em>Logging Levels</em></a> for a list of levels.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>The <em>lvl</em> parameter now accepts a string representation of the
level such as &#8216;INFO&#8217; as an alternative to the integer constants
such as <tt class="xref py py-const docutils literal"><span class="pre">INFO</span></tt>.</p>
</div>
</dd></dl>

<dl class="method">
<dt id="logging.Handler.setFormatter">
<tt class="descclassname">Handler.</tt><tt class="descname">setFormatter</tt><big>(</big><em>form</em><big>)</big><a class="headerlink" href="#logging.Handler.setFormatter" title="Permalink to this definition">¶</a></dt>
<dd><p>Sets the <a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><tt class="xref py py-class docutils literal"><span class="pre">Formatter</span></tt></a> for this handler to <em>form</em>.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Handler.addFilter">
<tt class="descclassname">Handler.</tt><tt class="descname">addFilter</tt><big>(</big><em>filt</em><big>)</big><a class="headerlink" href="#logging.Handler.addFilter" title="Permalink to this definition">¶</a></dt>
<dd><p>Adds the specified filter <em>filt</em> to this handler.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Handler.removeFilter">
<tt class="descclassname">Handler.</tt><tt class="descname">removeFilter</tt><big>(</big><em>filt</em><big>)</big><a class="headerlink" href="#logging.Handler.removeFilter" title="Permalink to this definition">¶</a></dt>
<dd><p>Removes the specified filter <em>filt</em> from this handler.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Handler.filter">
<tt class="descclassname">Handler.</tt><tt class="descname">filter</tt><big>(</big><em>record</em><big>)</big><a class="headerlink" href="#logging.Handler.filter" title="Permalink to this definition">¶</a></dt>
<dd><p>Applies this handler&#8217;s filters to the record and returns a true value if the
record is to be processed. The filters are consulted in turn, until one of
them returns a false value. If none of them return a false value, the record
will be emitted. If one returns a false value, the handler will not emit the
record.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Handler.flush">
<tt class="descclassname">Handler.</tt><tt class="descname">flush</tt><big>(</big><big>)</big><a class="headerlink" href="#logging.Handler.flush" title="Permalink to this definition">¶</a></dt>
<dd><p>Ensure all logging output has been flushed. This version does nothing and is
intended to be implemented by subclasses.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Handler.close">
<tt class="descclassname">Handler.</tt><tt class="descname">close</tt><big>(</big><big>)</big><a class="headerlink" href="#logging.Handler.close" title="Permalink to this definition">¶</a></dt>
<dd><p>Tidy up any resources used by the handler. This version does no output but
removes the handler from an internal list of handlers which is closed when
<a class="reference internal" href="#logging.shutdown" title="logging.shutdown"><tt class="xref py py-func docutils literal"><span class="pre">shutdown()</span></tt></a> is called. Subclasses should ensure that this gets called
from overridden <a class="reference internal" href="#logging.Handler.close" title="logging.Handler.close"><tt class="xref py py-meth docutils literal"><span class="pre">close()</span></tt></a> methods.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Handler.handle">
<tt class="descclassname">Handler.</tt><tt class="descname">handle</tt><big>(</big><em>record</em><big>)</big><a class="headerlink" href="#logging.Handler.handle" title="Permalink to this definition">¶</a></dt>
<dd><p>Conditionally emits the specified logging record, depending on filters which may
have been added to the handler. Wraps the actual emission of the record with
acquisition/release of the I/O thread lock.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Handler.handleError">
<tt class="descclassname">Handler.</tt><tt class="descname">handleError</tt><big>(</big><em>record</em><big>)</big><a class="headerlink" href="#logging.Handler.handleError" title="Permalink to this definition">¶</a></dt>
<dd><p>This method should be called from handlers when an exception is encountered
during an <a class="reference internal" href="#logging.Handler.emit" title="logging.Handler.emit"><tt class="xref py py-meth docutils literal"><span class="pre">emit()</span></tt></a> call. If the module-level attribute
<tt class="docutils literal"><span class="pre">raiseExceptions</span></tt> is <tt class="docutils literal"><span class="pre">False</span></tt>, exceptions get silently ignored. This is
what is mostly wanted for a logging system - most users will not care about
errors in the logging system, they are more interested in application
errors. You could, however, replace this with a custom handler if you wish.
The specified record is the one which was being processed when the exception
occurred. (The default value of <tt class="docutils literal"><span class="pre">raiseExceptions</span></tt> is <tt class="docutils literal"><span class="pre">True</span></tt>, as that is
more useful during development).</p>
</dd></dl>

<dl class="method">
<dt id="logging.Handler.format">
<tt class="descclassname">Handler.</tt><tt class="descname">format</tt><big>(</big><em>record</em><big>)</big><a class="headerlink" href="#logging.Handler.format" title="Permalink to this definition">¶</a></dt>
<dd><p>Do formatting for a record - if a formatter is set, use it. Otherwise, use the
default formatter for the module.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Handler.emit">
<tt class="descclassname">Handler.</tt><tt class="descname">emit</tt><big>(</big><em>record</em><big>)</big><a class="headerlink" href="#logging.Handler.emit" title="Permalink to this definition">¶</a></dt>
<dd><p>Do whatever it takes to actually log the specified logging record. This version
is intended to be implemented by subclasses and so raises a
<a class="reference internal" href="exceptions.html#NotImplementedError" title="NotImplementedError"><tt class="xref py py-exc docutils literal"><span class="pre">NotImplementedError</span></tt></a>.</p>
</dd></dl>

<p>For a list of handlers included as standard, see <a class="reference internal" href="logging.handlers.html#module-logging.handlers" title="logging.handlers: Handlers for the logging module."><tt class="xref py py-mod docutils literal"><span class="pre">logging.handlers</span></tt></a>.</p>
</div>
<div class="section" id="formatter-objects">
<span id="id1"></span><h2>16.6.4. Formatter Objects<a class="headerlink" href="#formatter-objects" title="Permalink to this headline">¶</a></h2>
<p><a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><tt class="xref py py-class docutils literal"><span class="pre">Formatter</span></tt></a> objects have the following attributes and methods. They are
responsible for converting a <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><tt class="xref py py-class docutils literal"><span class="pre">LogRecord</span></tt></a> to (usually) a string which can
be interpreted by either a human or an external system. The base
<a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><tt class="xref py py-class docutils literal"><span class="pre">Formatter</span></tt></a> allows a formatting string to be specified. If none is
supplied, the default value of <tt class="docutils literal"><span class="pre">'%(message)s'</span></tt> is used, which just includes
the message in the logging call. To have additional items of information in the
formatted output (such as a timestamp), keep reading.</p>
<p>A Formatter can be initialized with a format string which makes use of knowledge
of the <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><tt class="xref py py-class docutils literal"><span class="pre">LogRecord</span></tt></a> attributes - such as the default value mentioned above
making use of the fact that the user&#8217;s message and arguments are pre-formatted
into a <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><tt class="xref py py-class docutils literal"><span class="pre">LogRecord</span></tt></a>&#8216;s <em>message</em> attribute.  This format string contains
standard Python %-style mapping keys. See section <a class="reference internal" href="stdtypes.html#old-string-formatting"><em>printf-style String Formatting</em></a>
for more information on string formatting.</p>
<p>The useful mapping keys in a <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><tt class="xref py py-class docutils literal"><span class="pre">LogRecord</span></tt></a> are given in the section on
<a class="reference internal" href="#logrecord-attributes"><em>LogRecord attributes</em></a>.</p>
<dl class="class">
<dt id="logging.Formatter">
<em class="property">class </em><tt class="descclassname">logging.</tt><tt class="descname">Formatter</tt><big>(</big><em>fmt=None</em>, <em>datefmt=None</em>, <em>style='%'</em><big>)</big><a class="headerlink" href="#logging.Formatter" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns a new instance of the <a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><tt class="xref py py-class docutils literal"><span class="pre">Formatter</span></tt></a> class.  The instance is
initialized with a format string for the message as a whole, as well as a
format string for the date/time portion of a message.  If no <em>fmt</em> is
specified, <tt class="docutils literal"><span class="pre">'%(message)s'</span></tt> is used.  If no <em>datefmt</em> is specified, the
ISO8601 date format is used.</p>
<p>The <em>style</em> parameter can be one of &#8216;%&#8217;, &#8216;{&#8216; or &#8216;$&#8217; and determines how
the format string will be merged with its data: using one of %-formatting,
<a class="reference internal" href="stdtypes.html#str.format" title="str.format"><tt class="xref py py-meth docutils literal"><span class="pre">str.format()</span></tt></a> or <a class="reference internal" href="string.html#string.Template" title="string.Template"><tt class="xref py py-class docutils literal"><span class="pre">string.Template</span></tt></a>. See <a class="reference internal" href="../howto/logging-cookbook.html#formatting-styles"><em>Using particular formatting styles throughout your application</em></a>
for more information on using {- and $-formatting for log messages.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>The <em>style</em> parameter was added.</p>
</div>
<dl class="method">
<dt id="logging.Formatter.format">
<tt class="descname">format</tt><big>(</big><em>record</em><big>)</big><a class="headerlink" href="#logging.Formatter.format" title="Permalink to this definition">¶</a></dt>
<dd><p>The record&#8217;s attribute dictionary is used as the operand to a string
formatting operation. Returns the resulting string. Before formatting the
dictionary, a couple of preparatory steps are carried out. The <em>message</em>
attribute of the record is computed using <em>msg</em> % <em>args</em>. If the
formatting string contains <tt class="docutils literal"><span class="pre">'(asctime)'</span></tt>, <a class="reference internal" href="#logging.Formatter.formatTime" title="logging.Formatter.formatTime"><tt class="xref py py-meth docutils literal"><span class="pre">formatTime()</span></tt></a> is called
to format the event time. If there is exception information, it is
formatted using <a class="reference internal" href="#logging.Formatter.formatException" title="logging.Formatter.formatException"><tt class="xref py py-meth docutils literal"><span class="pre">formatException()</span></tt></a> and appended to the message. Note
that the formatted exception information is cached in attribute
<em>exc_text</em>. This is useful because the exception information can be
pickled and sent across the wire, but you should be careful if you have
more than one <a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><tt class="xref py py-class docutils literal"><span class="pre">Formatter</span></tt></a> subclass which customizes the formatting
of exception information. In this case, you will have to clear the cached
value after a formatter has done its formatting, so that the next
formatter to handle the event doesn&#8217;t use the cached value but
recalculates it afresh.</p>
<p>If stack information is available, it&#8217;s appended after the exception
information, using <a class="reference internal" href="#logging.Formatter.formatStack" title="logging.Formatter.formatStack"><tt class="xref py py-meth docutils literal"><span class="pre">formatStack()</span></tt></a> to transform it if necessary.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Formatter.formatTime">
<tt class="descname">formatTime</tt><big>(</big><em>record</em>, <em>datefmt=None</em><big>)</big><a class="headerlink" href="#logging.Formatter.formatTime" title="Permalink to this definition">¶</a></dt>
<dd><p>This method should be called from <a class="reference internal" href="functions.html#format" title="format"><tt class="xref py py-meth docutils literal"><span class="pre">format()</span></tt></a> by a formatter which
wants to make use of a formatted time. This method can be overridden in
formatters to provide for any specific requirement, but the basic behavior
is as follows: if <em>datefmt</em> (a string) is specified, it is used with
<a class="reference internal" href="time.html#time.strftime" title="time.strftime"><tt class="xref py py-func docutils literal"><span class="pre">time.strftime()</span></tt></a> to format the creation time of the
record. Otherwise, the ISO8601 format is used.  The resulting string is
returned.</p>
<p>This function uses a user-configurable function to convert the creation
time to a tuple. By default, <a class="reference internal" href="time.html#time.localtime" title="time.localtime"><tt class="xref py py-func docutils literal"><span class="pre">time.localtime()</span></tt></a> is used; to change
this for a particular formatter instance, set the <tt class="docutils literal"><span class="pre">converter</span></tt> attribute
to a function with the same signature as <a class="reference internal" href="time.html#time.localtime" title="time.localtime"><tt class="xref py py-func docutils literal"><span class="pre">time.localtime()</span></tt></a> or
<a class="reference internal" href="time.html#time.gmtime" title="time.gmtime"><tt class="xref py py-func docutils literal"><span class="pre">time.gmtime()</span></tt></a>. To change it for all formatters, for example if you
want all logging times to be shown in GMT, set the <tt class="docutils literal"><span class="pre">converter</span></tt>
attribute in the <tt class="docutils literal"><span class="pre">Formatter</span></tt> class.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.3: </span>Previously, the default ISO 8601 format was hard-coded as in this
example: <tt class="docutils literal"><span class="pre">2010-09-06</span> <span class="pre">22:38:15,292</span></tt> where the part before the comma is
handled by a strptime format string (<tt class="docutils literal"><span class="pre">'%Y-%m-%d</span> <span class="pre">%H:%M:%S'</span></tt>), and the
part after the comma is a millisecond value. Because strptime does not
have a format placeholder for milliseconds, the millisecond value is
appended using another format string, <tt class="docutils literal"><span class="pre">'%s,%03d'</span></tt> – and both of these
format strings have been hardcoded into this method. With the change,
these strings are defined as class-level attributes which can be
overridden at the instance level when desired. The names of the
attributes are <tt class="docutils literal"><span class="pre">default_time_format</span></tt> (for the strptime format string)
and <tt class="docutils literal"><span class="pre">default_msec_format</span></tt> (for appending the millisecond value).</p>
</div>
</dd></dl>

<dl class="method">
<dt id="logging.Formatter.formatException">
<tt class="descname">formatException</tt><big>(</big><em>exc_info</em><big>)</big><a class="headerlink" href="#logging.Formatter.formatException" title="Permalink to this definition">¶</a></dt>
<dd><p>Formats the specified exception information (a standard exception tuple as
returned by <a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><tt class="xref py py-func docutils literal"><span class="pre">sys.exc_info()</span></tt></a>) as a string. This default implementation
just uses <a class="reference internal" href="traceback.html#traceback.print_exception" title="traceback.print_exception"><tt class="xref py py-func docutils literal"><span class="pre">traceback.print_exception()</span></tt></a>. The resulting string is
returned.</p>
</dd></dl>

<dl class="method">
<dt id="logging.Formatter.formatStack">
<tt class="descname">formatStack</tt><big>(</big><em>stack_info</em><big>)</big><a class="headerlink" href="#logging.Formatter.formatStack" title="Permalink to this definition">¶</a></dt>
<dd><p>Formats the specified stack information (a string as returned by
<a class="reference internal" href="traceback.html#traceback.print_stack" title="traceback.print_stack"><tt class="xref py py-func docutils literal"><span class="pre">traceback.print_stack()</span></tt></a>, but with the last newline removed) as a
string. This default implementation just returns the input value.</p>
</dd></dl>

</dd></dl>

</div>
<div class="section" id="filter-objects">
<span id="filter"></span><h2>16.6.5. Filter Objects<a class="headerlink" href="#filter-objects" title="Permalink to this headline">¶</a></h2>
<p><tt class="docutils literal"><span class="pre">Filters</span></tt> can be used by <tt class="docutils literal"><span class="pre">Handlers</span></tt> and <tt class="docutils literal"><span class="pre">Loggers</span></tt> for more sophisticated
filtering than is provided by levels. The base filter class only allows events
which are below a certain point in the logger hierarchy. For example, a filter
initialized with &#8216;A.B&#8217; will allow events logged by loggers &#8216;A.B&#8217;, &#8216;A.B.C&#8217;,
&#8216;A.B.C.D&#8217;, &#8216;A.B.D&#8217; etc. but not &#8216;A.BB&#8217;, &#8216;B.A.B&#8217; etc. If initialized with the
empty string, all events are passed.</p>
<dl class="class">
<dt id="logging.Filter">
<em class="property">class </em><tt class="descclassname">logging.</tt><tt class="descname">Filter</tt><big>(</big><em>name=''</em><big>)</big><a class="headerlink" href="#logging.Filter" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns an instance of the <a class="reference internal" href="#logging.Filter" title="logging.Filter"><tt class="xref py py-class docutils literal"><span class="pre">Filter</span></tt></a> class. If <em>name</em> is specified, it
names a logger which, together with its children, will have its events allowed
through the filter. If <em>name</em> is the empty string, allows every event.</p>
<dl class="method">
<dt id="logging.Filter.filter">
<tt class="descname">filter</tt><big>(</big><em>record</em><big>)</big><a class="headerlink" href="#logging.Filter.filter" title="Permalink to this definition">¶</a></dt>
<dd><p>Is the specified record to be logged? Returns zero for no, nonzero for
yes. If deemed appropriate, the record may be modified in-place by this
method.</p>
</dd></dl>

</dd></dl>

<p>Note that filters attached to handlers are consulted before an event is
emitted by the handler, whereas filters attached to loggers are consulted
whenever an event is logged (using <a class="reference internal" href="#logging.debug" title="logging.debug"><tt class="xref py py-meth docutils literal"><span class="pre">debug()</span></tt></a>, <a class="reference internal" href="#logging.info" title="logging.info"><tt class="xref py py-meth docutils literal"><span class="pre">info()</span></tt></a>,
etc.), before sending an event to handlers. This means that events which have
been generated by descendant loggers will not be filtered by a logger&#8217;s filter
setting, unless the filter has also been applied to those descendant loggers.</p>
<p>You don&#8217;t actually need to subclass <tt class="docutils literal"><span class="pre">Filter</span></tt>: you can pass any instance
which has a <tt class="docutils literal"><span class="pre">filter</span></tt> method with the same semantics.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>You don&#8217;t need to create specialized <tt class="docutils literal"><span class="pre">Filter</span></tt> classes, or use other
classes with a <tt class="docutils literal"><span class="pre">filter</span></tt> method: you can use a function (or other
callable) as a filter. The filtering logic will check to see if the filter
object has a <tt class="docutils literal"><span class="pre">filter</span></tt> attribute: if it does, it&#8217;s assumed to be a
<tt class="docutils literal"><span class="pre">Filter</span></tt> and its <a class="reference internal" href="#logging.Filter.filter" title="logging.Filter.filter"><tt class="xref py py-meth docutils literal"><span class="pre">filter()</span></tt></a> method is called. Otherwise, it&#8217;s
assumed to be a callable and called with the record as the single
parameter. The returned value should conform to that returned by
<a class="reference internal" href="#logging.Filter.filter" title="logging.Filter.filter"><tt class="xref py py-meth docutils literal"><span class="pre">filter()</span></tt></a>.</p>
</div>
<p>Although filters are used primarily to filter records based on more
sophisticated criteria than levels, they get to see every record which is
processed by the handler or logger they&#8217;re attached to: this can be useful if
you want to do things like counting how many records were processed by a
particular logger or handler, or adding, changing or removing attributes in
the LogRecord being processed. Obviously changing the LogRecord needs to be
done with some care, but it does allow the injection of contextual information
into logs (see <a class="reference internal" href="../howto/logging-cookbook.html#filters-contextual"><em>Using Filters to impart contextual information</em></a>).</p>
</div>
<div class="section" id="logrecord-objects">
<span id="log-record"></span><h2>16.6.6. LogRecord Objects<a class="headerlink" href="#logrecord-objects" title="Permalink to this headline">¶</a></h2>
<p><a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><tt class="xref py py-class docutils literal"><span class="pre">LogRecord</span></tt></a> instances are created automatically by the <a class="reference internal" href="#logging.Logger" title="logging.Logger"><tt class="xref py py-class docutils literal"><span class="pre">Logger</span></tt></a>
every time something is logged, and can be created manually via
<a class="reference internal" href="#logging.makeLogRecord" title="logging.makeLogRecord"><tt class="xref py py-func docutils literal"><span class="pre">makeLogRecord()</span></tt></a> (for example, from a pickled event received over the
wire).</p>
<dl class="class">
<dt id="logging.LogRecord">
<em class="property">class </em><tt class="descclassname">logging.</tt><tt class="descname">LogRecord</tt><big>(</big><em>name</em>, <em>level</em>, <em>pathname</em>, <em>lineno</em>, <em>msg</em>, <em>args</em>, <em>exc_info</em>, <em>func=None</em>, <em>sinfo=None</em><big>)</big><a class="headerlink" href="#logging.LogRecord" title="Permalink to this definition">¶</a></dt>
<dd><p>Contains all the information pertinent to the event being logged.</p>
<p>The primary information is passed in <tt class="xref py py-attr docutils literal"><span class="pre">msg</span></tt> and <tt class="xref py py-attr docutils literal"><span class="pre">args</span></tt>, which
are combined using <tt class="docutils literal"><span class="pre">msg</span> <span class="pre">%</span> <span class="pre">args</span></tt> to create the <tt class="xref py py-attr docutils literal"><span class="pre">message</span></tt> field of the
record.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><ul class="first last simple">
<li><strong>name</strong> &#8211; The name of the logger used to log the event represented by
this LogRecord. Note that this name will always have this
value, even though it may be emitted by a handler attached to
a different (ancestor) logger.</li>
<li><strong>level</strong> &#8211; The numeric level of the logging event (one of DEBUG, INFO etc.)
Note that this is converted to <em>two</em> attributes of the LogRecord:
<tt class="docutils literal"><span class="pre">levelno</span></tt> for the numeric value and <tt class="docutils literal"><span class="pre">levelname</span></tt> for the
corresponding level name.</li>
<li><strong>pathname</strong> &#8211; The full pathname of the source file where the logging call
was made.</li>
<li><strong>lineno</strong> &#8211; The line number in the source file where the logging call was
made.</li>
<li><strong>msg</strong> &#8211; The event description message, possibly a format string with
placeholders for variable data.</li>
<li><strong>args</strong> &#8211; Variable data to merge into the <em>msg</em> argument to obtain the
event description.</li>
<li><strong>exc_info</strong> &#8211; An exception tuple with the current exception information,
or <em>None</em> if no exception information is available.</li>
<li><strong>func</strong> &#8211; The name of the function or method from which the logging call
was invoked.</li>
<li><strong>sinfo</strong> &#8211; A text string representing stack information from the base of
the stack in the current thread, up to the logging call.</li>
</ul>
</td>
</tr>
</tbody>
</table>
<dl class="method">
<dt id="logging.LogRecord.getMessage">
<tt class="descname">getMessage</tt><big>(</big><big>)</big><a class="headerlink" href="#logging.LogRecord.getMessage" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the message for this <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><tt class="xref py py-class docutils literal"><span class="pre">LogRecord</span></tt></a> instance after merging any
user-supplied arguments with the message. If the user-supplied message
argument to the logging call is not a string, <a class="reference internal" href="stdtypes.html#str" title="str"><tt class="xref py py-func docutils literal"><span class="pre">str()</span></tt></a> is called on it to
convert it to a string. This allows use of user-defined classes as
messages, whose <tt class="docutils literal"><span class="pre">__str__</span></tt> method can return the actual format string to
be used.</p>
</dd></dl>

<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>The creation of a <tt class="docutils literal"><span class="pre">LogRecord</span></tt> has been made more configurable by
providing a factory which is used to create the record. The factory can be
set using <a class="reference internal" href="#logging.getLogRecordFactory" title="logging.getLogRecordFactory"><tt class="xref py py-func docutils literal"><span class="pre">getLogRecordFactory()</span></tt></a> and <a class="reference internal" href="#logging.setLogRecordFactory" title="logging.setLogRecordFactory"><tt class="xref py py-func docutils literal"><span class="pre">setLogRecordFactory()</span></tt></a>
(see this for the factory&#8217;s signature).</p>
</div>
<p>This functionality can be used to inject your own values into a
LogRecord at creation time. You can use the following pattern:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">old_factory</span> <span class="o">=</span> <span class="n">logging</span><span class="o">.</span><span class="n">getLogRecordFactory</span><span class="p">()</span>

<span class="k">def</span> <span class="nf">record_factory</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">):</span>
    <span class="n">record</span> <span class="o">=</span> <span class="n">old_factory</span><span class="p">(</span><span class="o">*</span><span class="n">args</span><span class="p">,</span> <span class="o">**</span><span class="n">kwargs</span><span class="p">)</span>
    <span class="n">record</span><span class="o">.</span><span class="n">custom_attribute</span> <span class="o">=</span> <span class="mh">0xdecafbad</span>
    <span class="k">return</span> <span class="n">record</span>

<span class="n">logging</span><span class="o">.</span><span class="n">setLogRecordFactory</span><span class="p">(</span><span class="n">record_factory</span><span class="p">)</span>
</pre></div>
</div>
<p>With this pattern, multiple factories could be chained, and as long
as they don&#8217;t overwrite each other&#8217;s attributes or unintentionally
overwrite the standard attributes listed above, there should be no
surprises.</p>
</dd></dl>

</div>
<div class="section" id="logrecord-attributes">
<span id="id2"></span><h2>16.6.7. LogRecord attributes<a class="headerlink" href="#logrecord-attributes" title="Permalink to this headline">¶</a></h2>
<p>The LogRecord has a number of attributes, most of which are derived from the
parameters to the constructor. (Note that the names do not always correspond
exactly between the LogRecord constructor parameters and the LogRecord
attributes.) These attributes can be used to merge data from the record into
the format string. The following table lists (in alphabetical order) the
attribute names, their meanings and the corresponding placeholder in a %-style
format string.</p>
<p>If you are using {}-formatting (<a class="reference internal" href="stdtypes.html#str.format" title="str.format"><tt class="xref py py-func docutils literal"><span class="pre">str.format()</span></tt></a>), you can use
<tt class="docutils literal"><span class="pre">{attrname}</span></tt> as the placeholder in the format string. If you are using
$-formatting (<a class="reference internal" href="string.html#string.Template" title="string.Template"><tt class="xref py py-class docutils literal"><span class="pre">string.Template</span></tt></a>), use the form <tt class="docutils literal"><span class="pre">${attrname}</span></tt>. In
both cases, of course, replace <tt class="docutils literal"><span class="pre">attrname</span></tt> with the actual attribute name
you want to use.</p>
<p>In the case of {}-formatting, you can specify formatting flags by placing them
after the attribute name, separated from it with a colon. For example: a
placeholder of <tt class="docutils literal"><span class="pre">{msecs:03d}</span></tt> would format a millisecond value of <tt class="docutils literal"><span class="pre">4</span></tt> as
<tt class="docutils literal"><span class="pre">004</span></tt>. Refer to the <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><tt class="xref py py-meth docutils literal"><span class="pre">str.format()</span></tt></a> documentation for full details on
the options available to you.</p>
<table border="1" class="docutils">
<colgroup>
<col width="18%" />
<col width="28%" />
<col width="53%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Attribute name</th>
<th class="head">Format</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td>args</td>
<td>You shouldn&#8217;t need to
format this yourself.</td>
<td>The tuple of arguments merged into <tt class="docutils literal"><span class="pre">msg</span></tt> to
produce <tt class="docutils literal"><span class="pre">message</span></tt>.</td>
</tr>
<tr class="row-odd"><td>asctime</td>
<td><tt class="docutils literal"><span class="pre">%(asctime)s</span></tt></td>
<td>Human-readable time when the
<a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><tt class="xref py py-class docutils literal"><span class="pre">LogRecord</span></tt></a> was created.  By default
this is of the form &#8216;2003-07-08 16:49:45,896&#8217;
(the numbers after the comma are millisecond
portion of the time).</td>
</tr>
<tr class="row-even"><td>created</td>
<td><tt class="docutils literal"><span class="pre">%(created)f</span></tt></td>
<td>Time when the <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><tt class="xref py py-class docutils literal"><span class="pre">LogRecord</span></tt></a> was created
(as returned by <a class="reference internal" href="time.html#time.time" title="time.time"><tt class="xref py py-func docutils literal"><span class="pre">time.time()</span></tt></a>).</td>
</tr>
<tr class="row-odd"><td>exc_info</td>
<td>You shouldn&#8217;t need to
format this yourself.</td>
<td>Exception tuple (à la <tt class="docutils literal"><span class="pre">sys.exc_info</span></tt>) or,
if no exception has occurred, <em>None</em>.</td>
</tr>
<tr class="row-even"><td>filename</td>
<td><tt class="docutils literal"><span class="pre">%(filename)s</span></tt></td>
<td>Filename portion of <tt class="docutils literal"><span class="pre">pathname</span></tt>.</td>
</tr>
<tr class="row-odd"><td>funcName</td>
<td><tt class="docutils literal"><span class="pre">%(funcName)s</span></tt></td>
<td>Name of function containing the logging call.</td>
</tr>
<tr class="row-even"><td>levelname</td>
<td><tt class="docutils literal"><span class="pre">%(levelname)s</span></tt></td>
<td>Text logging level for the message
(<tt class="docutils literal"><span class="pre">'DEBUG'</span></tt>, <tt class="docutils literal"><span class="pre">'INFO'</span></tt>, <tt class="docutils literal"><span class="pre">'WARNING'</span></tt>,
<tt class="docutils literal"><span class="pre">'ERROR'</span></tt>, <tt class="docutils literal"><span class="pre">'CRITICAL'</span></tt>).</td>
</tr>
<tr class="row-odd"><td>levelno</td>
<td><tt class="docutils literal"><span class="pre">%(levelno)s</span></tt></td>
<td>Numeric logging level for the message
(<tt class="xref py py-const docutils literal"><span class="pre">DEBUG</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">INFO</span></tt>,
<tt class="xref py py-const docutils literal"><span class="pre">WARNING</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">ERROR</span></tt>,
<tt class="xref py py-const docutils literal"><span class="pre">CRITICAL</span></tt>).</td>
</tr>
<tr class="row-even"><td>lineno</td>
<td><tt class="docutils literal"><span class="pre">%(lineno)d</span></tt></td>
<td>Source line number where the logging call was
issued (if available).</td>
</tr>
<tr class="row-odd"><td>module</td>
<td><tt class="docutils literal"><span class="pre">%(module)s</span></tt></td>
<td>Module (name portion of <tt class="docutils literal"><span class="pre">filename</span></tt>).</td>
</tr>
<tr class="row-even"><td>msecs</td>
<td><tt class="docutils literal"><span class="pre">%(msecs)d</span></tt></td>
<td>Millisecond portion of the time when the
<a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><tt class="xref py py-class docutils literal"><span class="pre">LogRecord</span></tt></a> was created.</td>
</tr>
<tr class="row-odd"><td>message</td>
<td><tt class="docutils literal"><span class="pre">%(message)s</span></tt></td>
<td>The logged message, computed as <tt class="docutils literal"><span class="pre">msg</span> <span class="pre">%</span>
<span class="pre">args</span></tt>. This is set when
<a class="reference internal" href="#logging.Formatter.format" title="logging.Formatter.format"><tt class="xref py py-meth docutils literal"><span class="pre">Formatter.format()</span></tt></a> is invoked.</td>
</tr>
<tr class="row-even"><td>msg</td>
<td>You shouldn&#8217;t need to
format this yourself.</td>
<td>The format string passed in the original
logging call. Merged with <tt class="docutils literal"><span class="pre">args</span></tt> to
produce <tt class="docutils literal"><span class="pre">message</span></tt>, or an arbitrary object
(see <a class="reference internal" href="../howto/logging.html#arbitrary-object-messages"><em>Using arbitrary objects as messages</em></a>).</td>
</tr>
<tr class="row-odd"><td>name</td>
<td><tt class="docutils literal"><span class="pre">%(name)s</span></tt></td>
<td>Name of the logger used to log the call.</td>
</tr>
<tr class="row-even"><td>pathname</td>
<td><tt class="docutils literal"><span class="pre">%(pathname)s</span></tt></td>
<td>Full pathname of the source file where the
logging call was issued (if available).</td>
</tr>
<tr class="row-odd"><td>process</td>
<td><tt class="docutils literal"><span class="pre">%(process)d</span></tt></td>
<td>Process ID (if available).</td>
</tr>
<tr class="row-even"><td>processName</td>
<td><tt class="docutils literal"><span class="pre">%(processName)s</span></tt></td>
<td>Process name (if available).</td>
</tr>
<tr class="row-odd"><td>relativeCreated</td>
<td><tt class="docutils literal"><span class="pre">%(relativeCreated)d</span></tt></td>
<td>Time in milliseconds when the LogRecord was
created, relative to the time the logging
module was loaded.</td>
</tr>
<tr class="row-even"><td>stack_info</td>
<td>You shouldn&#8217;t need to
format this yourself.</td>
<td>Stack frame information (where available)
from the bottom of the stack in the current
thread, up to and including the stack frame
of the logging call which resulted in the
creation of this record.</td>
</tr>
<tr class="row-odd"><td>thread</td>
<td><tt class="docutils literal"><span class="pre">%(thread)d</span></tt></td>
<td>Thread ID (if available).</td>
</tr>
<tr class="row-even"><td>threadName</td>
<td><tt class="docutils literal"><span class="pre">%(threadName)s</span></tt></td>
<td>Thread name (if available).</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.1: </span><em>processName</em> was added.</p>
</div>
</div>
<div class="section" id="loggeradapter-objects">
<span id="logger-adapter"></span><h2>16.6.8. LoggerAdapter Objects<a class="headerlink" href="#loggeradapter-objects" title="Permalink to this headline">¶</a></h2>
<p><a class="reference internal" href="#logging.LoggerAdapter" title="logging.LoggerAdapter"><tt class="xref py py-class docutils literal"><span class="pre">LoggerAdapter</span></tt></a> instances are used to conveniently pass contextual
information into logging calls. For a usage example, see the section on
<a class="reference internal" href="../howto/logging-cookbook.html#context-info"><em>adding contextual information to your logging output</em></a>.</p>
<dl class="class">
<dt id="logging.LoggerAdapter">
<em class="property">class </em><tt class="descclassname">logging.</tt><tt class="descname">LoggerAdapter</tt><big>(</big><em>logger</em>, <em>extra</em><big>)</big><a class="headerlink" href="#logging.LoggerAdapter" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns an instance of <a class="reference internal" href="#logging.LoggerAdapter" title="logging.LoggerAdapter"><tt class="xref py py-class docutils literal"><span class="pre">LoggerAdapter</span></tt></a> initialized with an
underlying <a class="reference internal" href="#logging.Logger" title="logging.Logger"><tt class="xref py py-class docutils literal"><span class="pre">Logger</span></tt></a> instance and a dict-like object.</p>
<dl class="method">
<dt id="logging.LoggerAdapter.process">
<tt class="descname">process</tt><big>(</big><em>msg</em>, <em>kwargs</em><big>)</big><a class="headerlink" href="#logging.LoggerAdapter.process" title="Permalink to this definition">¶</a></dt>
<dd><p>Modifies the message and/or keyword arguments passed to a logging call in
order to insert contextual information. This implementation takes the object
passed as <em>extra</em> to the constructor and adds it to <em>kwargs</em> using key
&#8216;extra&#8217;. The return value is a (<em>msg</em>, <em>kwargs</em>) tuple which has the
(possibly modified) versions of the arguments passed in.</p>
</dd></dl>

</dd></dl>

<p>In addition to the above, <a class="reference internal" href="#logging.LoggerAdapter" title="logging.LoggerAdapter"><tt class="xref py py-class docutils literal"><span class="pre">LoggerAdapter</span></tt></a> supports the following
methods of <a class="reference internal" href="#logging.Logger" title="logging.Logger"><tt class="xref py py-class docutils literal"><span class="pre">Logger</span></tt></a>: <a class="reference internal" href="#logging.Logger.debug" title="logging.Logger.debug"><tt class="xref py py-meth docutils literal"><span class="pre">debug()</span></tt></a>, <a class="reference internal" href="#logging.Logger.info" title="logging.Logger.info"><tt class="xref py py-meth docutils literal"><span class="pre">info()</span></tt></a>,
<a class="reference internal" href="#logging.Logger.warning" title="logging.Logger.warning"><tt class="xref py py-meth docutils literal"><span class="pre">warning()</span></tt></a>, <a class="reference internal" href="#logging.Logger.error" title="logging.Logger.error"><tt class="xref py py-meth docutils literal"><span class="pre">error()</span></tt></a>, <a class="reference internal" href="#logging.Logger.exception" title="logging.Logger.exception"><tt class="xref py py-meth docutils literal"><span class="pre">exception()</span></tt></a>,
<a class="reference internal" href="#logging.Logger.critical" title="logging.Logger.critical"><tt class="xref py py-meth docutils literal"><span class="pre">critical()</span></tt></a>, <a class="reference internal" href="#logging.Logger.log" title="logging.Logger.log"><tt class="xref py py-meth docutils literal"><span class="pre">log()</span></tt></a>, <a class="reference internal" href="#logging.Logger.isEnabledFor" title="logging.Logger.isEnabledFor"><tt class="xref py py-meth docutils literal"><span class="pre">isEnabledFor()</span></tt></a>,
<a class="reference internal" href="#logging.Logger.getEffectiveLevel" title="logging.Logger.getEffectiveLevel"><tt class="xref py py-meth docutils literal"><span class="pre">getEffectiveLevel()</span></tt></a>, <a class="reference internal" href="#logging.Logger.setLevel" title="logging.Logger.setLevel"><tt class="xref py py-meth docutils literal"><span class="pre">setLevel()</span></tt></a> and
<a class="reference internal" href="#logging.Logger.hasHandlers" title="logging.Logger.hasHandlers"><tt class="xref py py-meth docutils literal"><span class="pre">hasHandlers()</span></tt></a>. These methods have the same signatures as their
counterparts in <a class="reference internal" href="#logging.Logger" title="logging.Logger"><tt class="xref py py-class docutils literal"><span class="pre">Logger</span></tt></a>, so you can use the two types of instances
interchangeably.</p>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>The <a class="reference internal" href="#logging.Logger.isEnabledFor" title="logging.Logger.isEnabledFor"><tt class="xref py py-meth docutils literal"><span class="pre">isEnabledFor()</span></tt></a>, <a class="reference internal" href="#logging.Logger.getEffectiveLevel" title="logging.Logger.getEffectiveLevel"><tt class="xref py py-meth docutils literal"><span class="pre">getEffectiveLevel()</span></tt></a>,
<a class="reference internal" href="#logging.Logger.setLevel" title="logging.Logger.setLevel"><tt class="xref py py-meth docutils literal"><span class="pre">setLevel()</span></tt></a> and <a class="reference internal" href="#logging.Logger.hasHandlers" title="logging.Logger.hasHandlers"><tt class="xref py py-meth docutils literal"><span class="pre">hasHandlers()</span></tt></a> methods were added
to <a class="reference internal" href="#logging.LoggerAdapter" title="logging.LoggerAdapter"><tt class="xref py py-class docutils literal"><span class="pre">LoggerAdapter</span></tt></a>.  These methods delegate to the underlying logger.</p>
</div>
</div>
<div class="section" id="thread-safety">
<h2>16.6.9. Thread Safety<a class="headerlink" href="#thread-safety" title="Permalink to this headline">¶</a></h2>
<p>The logging module is intended to be thread-safe without any special work
needing to be done by its clients. It achieves this though using threading
locks; there is one lock to serialize access to the module&#8217;s shared data, and
each handler also creates a lock to serialize access to its underlying I/O.</p>
<p>If you are implementing asynchronous signal handlers using the <a class="reference internal" href="signal.html#module-signal" title="signal: Set handlers for asynchronous events."><tt class="xref py py-mod docutils literal"><span class="pre">signal</span></tt></a>
module, you may not be able to use logging from within such handlers. This is
because lock implementations in the <a class="reference internal" href="threading.html#module-threading" title="threading: Thread-based parallelism."><tt class="xref py py-mod docutils literal"><span class="pre">threading</span></tt></a> module are not always
re-entrant, and so cannot be invoked from such signal handlers.</p>
</div>
<div class="section" id="module-level-functions">
<h2>16.6.10. Module-Level Functions<a class="headerlink" href="#module-level-functions" title="Permalink to this headline">¶</a></h2>
<p>In addition to the classes described above, there are a number of module- level
functions.</p>
<dl class="function">
<dt id="logging.getLogger">
<tt class="descclassname">logging.</tt><tt class="descname">getLogger</tt><big>(</big><em>name=None</em><big>)</big><a class="headerlink" href="#logging.getLogger" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a logger with the specified name or, if name is <tt class="docutils literal"><span class="pre">None</span></tt>, return a
logger which is the root logger of the hierarchy. If specified, the name is
typically a dot-separated hierarchical name like <em>&#8216;a&#8217;</em>, <em>&#8216;a.b&#8217;</em> or <em>&#8216;a.b.c.d&#8217;</em>.
Choice of these names is entirely up to the developer who is using logging.</p>
<p>All calls to this function with a given name return the same logger instance.
This means that logger instances never need to be passed between different parts
of an application.</p>
</dd></dl>

<dl class="function">
<dt id="logging.getLoggerClass">
<tt class="descclassname">logging.</tt><tt class="descname">getLoggerClass</tt><big>(</big><big>)</big><a class="headerlink" href="#logging.getLoggerClass" title="Permalink to this definition">¶</a></dt>
<dd><p>Return either the standard <a class="reference internal" href="#logging.Logger" title="logging.Logger"><tt class="xref py py-class docutils literal"><span class="pre">Logger</span></tt></a> class, or the last class passed to
<a class="reference internal" href="#logging.setLoggerClass" title="logging.setLoggerClass"><tt class="xref py py-func docutils literal"><span class="pre">setLoggerClass()</span></tt></a>. This function may be called from within a new class
definition, to ensure that installing a customized <a class="reference internal" href="#logging.Logger" title="logging.Logger"><tt class="xref py py-class docutils literal"><span class="pre">Logger</span></tt></a> class will
not undo customizations already applied by other code. For example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="k">class</span> <span class="nc">MyLogger</span><span class="p">(</span><span class="n">logging</span><span class="o">.</span><span class="n">getLoggerClass</span><span class="p">()):</span>
    <span class="c"># ... override behaviour here</span>
</pre></div>
</div>
</dd></dl>

<dl class="function">
<dt id="logging.getLogRecordFactory">
<tt class="descclassname">logging.</tt><tt class="descname">getLogRecordFactory</tt><big>(</big><big>)</big><a class="headerlink" href="#logging.getLogRecordFactory" title="Permalink to this definition">¶</a></dt>
<dd><p>Return a callable which is used to create a <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><tt class="xref py py-class docutils literal"><span class="pre">LogRecord</span></tt></a>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2: </span>This function has been provided, along with <a class="reference internal" href="#logging.setLogRecordFactory" title="logging.setLogRecordFactory"><tt class="xref py py-func docutils literal"><span class="pre">setLogRecordFactory()</span></tt></a>,
to allow developers more control over how the <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><tt class="xref py py-class docutils literal"><span class="pre">LogRecord</span></tt></a>
representing a logging event is constructed.</p>
</div>
<p>See <a class="reference internal" href="#logging.setLogRecordFactory" title="logging.setLogRecordFactory"><tt class="xref py py-func docutils literal"><span class="pre">setLogRecordFactory()</span></tt></a> for more information about the how the
factory is called.</p>
</dd></dl>

<dl class="function">
<dt id="logging.debug">
<tt class="descclassname">logging.</tt><tt class="descname">debug</tt><big>(</big><em>msg</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#logging.debug" title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <tt class="xref py py-const docutils literal"><span class="pre">DEBUG</span></tt> on the root logger. The <em>msg</em> is the
message format string, and the <em>args</em> are the arguments which are merged into
<em>msg</em> using the string formatting operator. (Note that this means that you can
use keywords in the format string, together with a single dictionary argument.)</p>
<p>There are three keyword arguments in <em>kwargs</em> which are inspected: <em>exc_info</em>
which, if it does not evaluate as false, causes exception information to be
added to the logging message. If an exception tuple (in the format returned by
<a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><tt class="xref py py-func docutils literal"><span class="pre">sys.exc_info()</span></tt></a>) is provided, it is used; otherwise, <a class="reference internal" href="sys.html#sys.exc_info" title="sys.exc_info"><tt class="xref py py-func docutils literal"><span class="pre">sys.exc_info()</span></tt></a>
is called to get the exception information.</p>
<p>The second optional keyword argument is <em>stack_info</em>, which defaults to
<tt class="docutils literal"><span class="pre">False</span></tt>. If true, stack information is added to the logging
message, including the actual logging call. Note that this is not the same
stack information as that displayed through specifying <em>exc_info</em>: The
former is stack frames from the bottom of the stack up to the logging call
in the current thread, whereas the latter is information about stack frames
which have been unwound, following an exception, while searching for
exception handlers.</p>
<p>You can specify <em>stack_info</em> independently of <em>exc_info</em>, e.g. to just show
how you got to a certain point in your code, even when no exceptions were
raised. The stack frames are printed following a header line which says:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">Stack</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
</pre></div>
</div>
<p>This mimics the <tt class="docutils literal"><span class="pre">Traceback</span> <span class="pre">(most</span> <span class="pre">recent</span> <span class="pre">call</span> <span class="pre">last):</span></tt> which is used when
displaying exception frames.</p>
<p>The third optional keyword argument is <em>extra</em> which can be used to pass a
dictionary which is used to populate the __dict__ of the LogRecord created for
the logging event with user-defined attributes. These custom attributes can then
be used as you like. For example, they could be incorporated into logged
messages. For example:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="n">FORMAT</span> <span class="o">=</span> <span class="s">&#39;%(asctime)-15s %(clientip)s %(user)-8s %(message)s&#39;</span>
<span class="n">logging</span><span class="o">.</span><span class="n">basicConfig</span><span class="p">(</span><span class="nb">format</span><span class="o">=</span><span class="n">FORMAT</span><span class="p">)</span>
<span class="n">d</span> <span class="o">=</span> <span class="p">{</span><span class="s">&#39;clientip&#39;</span><span class="p">:</span> <span class="s">&#39;192.168.0.1&#39;</span><span class="p">,</span> <span class="s">&#39;user&#39;</span><span class="p">:</span> <span class="s">&#39;fbloggs&#39;</span><span class="p">}</span>
<span class="n">logging</span><span class="o">.</span><span class="n">warning</span><span class="p">(</span><span class="s">&#39;Protocol problem: %s&#39;</span><span class="p">,</span> <span class="s">&#39;connection reset&#39;</span><span class="p">,</span> <span class="n">extra</span><span class="o">=</span><span class="n">d</span><span class="p">)</span>
</pre></div>
</div>
<p>would print something like:</p>
<div class="highlight-python3"><div class="highlight"><pre><span class="mi">2006</span><span class="o">-</span><span class="mi">02</span><span class="o">-</span><span class="mi">08</span> <span class="mi">22</span><span class="p">:</span><span class="mi">20</span><span class="p">:</span><span class="mi">02</span><span class="p">,</span><span class="mi">165</span> <span class="mf">192.168</span><span class="o">.</span><span class="mf">0.1</span> <span class="n">fbloggs</span>  <span class="n">Protocol</span> <span class="n">problem</span><span class="p">:</span> <span class="n">connection</span> <span class="n">reset</span>
</pre></div>
</div>
<p>The keys in the dictionary passed in <em>extra</em> should not clash with the keys used
by the logging system. (See the <a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><tt class="xref py py-class docutils literal"><span class="pre">Formatter</span></tt></a> documentation for more
information on which keys are used by the logging system.)</p>
<p>If you choose to use these attributes in logged messages, you need to exercise
some care. In the above example, for instance, the <a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><tt class="xref py py-class docutils literal"><span class="pre">Formatter</span></tt></a> has been
set up with a format string which expects &#8216;clientip&#8217; and &#8216;user&#8217; in the attribute
dictionary of the LogRecord. If these are missing, the message will not be
logged because a string formatting exception will occur. So in this case, you
always need to pass the <em>extra</em> dictionary with these keys.</p>
<p>While this might be annoying, this feature is intended for use in specialized
circumstances, such as multi-threaded servers where the same code executes in
many contexts, and interesting conditions which arise are dependent on this
context (such as remote client IP address and authenticated user name, in the
above example). In such circumstances, it is likely that specialized
<a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><tt class="xref py py-class docutils literal"><span class="pre">Formatter</span></tt></a>s would be used with particular <tt class="xref py py-class docutils literal"><span class="pre">Handler</span></tt>s.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2: </span>The <em>stack_info</em> parameter was added.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="logging.info">
<tt class="descclassname">logging.</tt><tt class="descname">info</tt><big>(</big><em>msg</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#logging.info" title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <tt class="xref py py-const docutils literal"><span class="pre">INFO</span></tt> on the root logger. The arguments are
interpreted as for <a class="reference internal" href="#logging.debug" title="logging.debug"><tt class="xref py py-func docutils literal"><span class="pre">debug()</span></tt></a>.</p>
</dd></dl>

<dl class="function">
<dt id="logging.warning">
<tt class="descclassname">logging.</tt><tt class="descname">warning</tt><big>(</big><em>msg</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#logging.warning" title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <tt class="xref py py-const docutils literal"><span class="pre">WARNING</span></tt> on the root logger. The arguments
are interpreted as for <a class="reference internal" href="#logging.debug" title="logging.debug"><tt class="xref py py-func docutils literal"><span class="pre">debug()</span></tt></a>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">There is an obsolete function <tt class="docutils literal"><span class="pre">warn</span></tt> which is functionally
identical to <tt class="docutils literal"><span class="pre">warning</span></tt>. As <tt class="docutils literal"><span class="pre">warn</span></tt> is deprecated, please do not use
it - use <tt class="docutils literal"><span class="pre">warning</span></tt> instead.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="logging.error">
<tt class="descclassname">logging.</tt><tt class="descname">error</tt><big>(</big><em>msg</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#logging.error" title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <tt class="xref py py-const docutils literal"><span class="pre">ERROR</span></tt> on the root logger. The arguments are
interpreted as for <a class="reference internal" href="#logging.debug" title="logging.debug"><tt class="xref py py-func docutils literal"><span class="pre">debug()</span></tt></a>.</p>
</dd></dl>

<dl class="function">
<dt id="logging.critical">
<tt class="descclassname">logging.</tt><tt class="descname">critical</tt><big>(</big><em>msg</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#logging.critical" title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <tt class="xref py py-const docutils literal"><span class="pre">CRITICAL</span></tt> on the root logger. The arguments
are interpreted as for <a class="reference internal" href="#logging.debug" title="logging.debug"><tt class="xref py py-func docutils literal"><span class="pre">debug()</span></tt></a>.</p>
</dd></dl>

<dl class="function">
<dt id="logging.exception">
<tt class="descclassname">logging.</tt><tt class="descname">exception</tt><big>(</big><em>msg</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#logging.exception" title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <tt class="xref py py-const docutils literal"><span class="pre">ERROR</span></tt> on the root logger. The arguments are
interpreted as for <a class="reference internal" href="#logging.debug" title="logging.debug"><tt class="xref py py-func docutils literal"><span class="pre">debug()</span></tt></a>. Exception info is added to the logging
message. This function should only be called from an exception handler.</p>
</dd></dl>

<dl class="function">
<dt id="logging.log">
<tt class="descclassname">logging.</tt><tt class="descname">log</tt><big>(</big><em>level</em>, <em>msg</em>, <em>*args</em>, <em>**kwargs</em><big>)</big><a class="headerlink" href="#logging.log" title="Permalink to this definition">¶</a></dt>
<dd><p>Logs a message with level <em>level</em> on the root logger. The other arguments are
interpreted as for <a class="reference internal" href="#logging.debug" title="logging.debug"><tt class="xref py py-func docutils literal"><span class="pre">debug()</span></tt></a>.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">The above module-level convenience functions, which delegate to the
root logger, call <a class="reference internal" href="#logging.basicConfig" title="logging.basicConfig"><tt class="xref py py-func docutils literal"><span class="pre">basicConfig()</span></tt></a> to ensure that at least one handler
is available. Because of this, they should <em>not</em> be used in threads,
in versions of Python earlier than 2.7.1 and 3.2, unless at least one
handler has been added to the root logger <em>before</em> the threads are
started. In earlier versions of Python, due to a thread safety shortcoming
in <a class="reference internal" href="#logging.basicConfig" title="logging.basicConfig"><tt class="xref py py-func docutils literal"><span class="pre">basicConfig()</span></tt></a>, this can (under rare circumstances) lead to
handlers being added multiple times to the root logger, which can in turn
lead to multiple messages for the same event.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="logging.disable">
<tt class="descclassname">logging.</tt><tt class="descname">disable</tt><big>(</big><em>lvl</em><big>)</big><a class="headerlink" href="#logging.disable" title="Permalink to this definition">¶</a></dt>
<dd><p>Provides an overriding level <em>lvl</em> for all loggers which takes precedence over
the logger&#8217;s own level. When the need arises to temporarily throttle logging
output down across the whole application, this function can be useful. Its
effect is to disable all logging calls of severity <em>lvl</em> and below, so that
if you call it with a value of INFO, then all INFO and DEBUG events would be
discarded, whereas those of severity WARNING and above would be processed
according to the logger&#8217;s effective level. If
<tt class="docutils literal"><span class="pre">logging.disable(logging.NOTSET)</span></tt> is called, it effectively removes this
overriding level, so that logging output again depends on the effective
levels of individual loggers.</p>
</dd></dl>

<dl class="function">
<dt id="logging.addLevelName">
<tt class="descclassname">logging.</tt><tt class="descname">addLevelName</tt><big>(</big><em>lvl</em>, <em>levelName</em><big>)</big><a class="headerlink" href="#logging.addLevelName" title="Permalink to this definition">¶</a></dt>
<dd><p>Associates level <em>lvl</em> with text <em>levelName</em> in an internal dictionary, which is
used to map numeric levels to a textual representation, for example when a
<a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><tt class="xref py py-class docutils literal"><span class="pre">Formatter</span></tt></a> formats a message. This function can also be used to define
your own levels. The only constraints are that all levels used must be
registered using this function, levels should be positive integers and they
should increase in increasing order of severity.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">If you are thinking of defining your own levels, please see the
section on <a class="reference internal" href="../howto/logging.html#custom-levels"><em>Custom Levels</em></a>.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="logging.getLevelName">
<tt class="descclassname">logging.</tt><tt class="descname">getLevelName</tt><big>(</big><em>lvl</em><big>)</big><a class="headerlink" href="#logging.getLevelName" title="Permalink to this definition">¶</a></dt>
<dd><p>Returns the textual representation of logging level <em>lvl</em>. If the level is one
of the predefined levels <tt class="xref py py-const docutils literal"><span class="pre">CRITICAL</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">ERROR</span></tt>, <tt class="xref py py-const docutils literal"><span class="pre">WARNING</span></tt>,
<tt class="xref py py-const docutils literal"><span class="pre">INFO</span></tt> or <tt class="xref py py-const docutils literal"><span class="pre">DEBUG</span></tt> then you get the corresponding string. If you
have associated levels with names using <a class="reference internal" href="#logging.addLevelName" title="logging.addLevelName"><tt class="xref py py-func docutils literal"><span class="pre">addLevelName()</span></tt></a> then the name you
have associated with <em>lvl</em> is returned. If a numeric value corresponding to one
of the defined levels is passed in, the corresponding string representation is
returned. Otherwise, the string &#8216;Level %s&#8217; % lvl is returned.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">Levels are internally integers (as they need to be compared in the
logging logic). This function is used to convert between an integer level
and the level name displayed in the formatted log output by means of the
<tt class="docutils literal"><span class="pre">%(levelname)s</span></tt> format specifier (see <a class="reference internal" href="#logrecord-attributes"><em>LogRecord attributes</em></a>).</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.4: </span>In Python versions earlier than 3.4, this function could also be passed a
text level, and would return the corresponding numeric value of the level.
This undocumented behaviour was considered a mistake, and was removed in
Python 3.4, but reinstated in 3.4.2 due to retain backward compatibility.</p>
</div>
</dd></dl>

<dl class="function">
<dt id="logging.makeLogRecord">
<tt class="descclassname">logging.</tt><tt class="descname">makeLogRecord</tt><big>(</big><em>attrdict</em><big>)</big><a class="headerlink" href="#logging.makeLogRecord" title="Permalink to this definition">¶</a></dt>
<dd><p>Creates and returns a new <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><tt class="xref py py-class docutils literal"><span class="pre">LogRecord</span></tt></a> instance whose attributes are
defined by <em>attrdict</em>. This function is useful for taking a pickled
<a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><tt class="xref py py-class docutils literal"><span class="pre">LogRecord</span></tt></a> attribute dictionary, sent over a socket, and reconstituting
it as a <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><tt class="xref py py-class docutils literal"><span class="pre">LogRecord</span></tt></a> instance at the receiving end.</p>
</dd></dl>

<dl class="function">
<dt id="logging.basicConfig">
<tt class="descclassname">logging.</tt><tt class="descname">basicConfig</tt><big>(</big><em>**kwargs</em><big>)</big><a class="headerlink" href="#logging.basicConfig" title="Permalink to this definition">¶</a></dt>
<dd><p>Does basic configuration for the logging system by creating a
<a class="reference internal" href="logging.handlers.html#logging.StreamHandler" title="logging.StreamHandler"><tt class="xref py py-class docutils literal"><span class="pre">StreamHandler</span></tt></a> with a default <a class="reference internal" href="#logging.Formatter" title="logging.Formatter"><tt class="xref py py-class docutils literal"><span class="pre">Formatter</span></tt></a> and adding it to the
root logger. The functions <a class="reference internal" href="#logging.debug" title="logging.debug"><tt class="xref py py-func docutils literal"><span class="pre">debug()</span></tt></a>, <a class="reference internal" href="#logging.info" title="logging.info"><tt class="xref py py-func docutils literal"><span class="pre">info()</span></tt></a>, <a class="reference internal" href="#logging.warning" title="logging.warning"><tt class="xref py py-func docutils literal"><span class="pre">warning()</span></tt></a>,
<a class="reference internal" href="#logging.error" title="logging.error"><tt class="xref py py-func docutils literal"><span class="pre">error()</span></tt></a> and <a class="reference internal" href="#logging.critical" title="logging.critical"><tt class="xref py py-func docutils literal"><span class="pre">critical()</span></tt></a> will call <a class="reference internal" href="#logging.basicConfig" title="logging.basicConfig"><tt class="xref py py-func docutils literal"><span class="pre">basicConfig()</span></tt></a> automatically
if no handlers are defined for the root logger.</p>
<p>This function does nothing if the root logger already has handlers
configured for it.</p>
<div class="admonition note">
<p class="first admonition-title">Note</p>
<p class="last">This function should be called from the main thread
before other threads are started. In versions of Python prior to
2.7.1 and 3.2, if this function is called from multiple threads,
it is possible (in rare circumstances) that a handler will be added
to the root logger more than once, leading to unexpected results
such as messages being duplicated in the log.</p>
</div>
<p>The following keyword arguments are supported.</p>
<table border="1" class="docutils">
<colgroup>
<col width="24%" />
<col width="76%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Format</th>
<th class="head">Description</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">filename</span></tt></td>
<td>Specifies that a FileHandler be created,
using the specified filename, rather than a
StreamHandler.</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">filemode</span></tt></td>
<td>Specifies the mode to open the file, if
filename is specified (if filemode is
unspecified, it defaults to &#8216;a&#8217;).</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">format</span></tt></td>
<td>Use the specified format string for the
handler.</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">datefmt</span></tt></td>
<td>Use the specified date/time format.</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">style</span></tt></td>
<td>If <tt class="docutils literal"><span class="pre">format</span></tt> is specified, use this style
for the format string. One of &#8216;%&#8217;, &#8216;{&#8216; or
&#8216;$&#8217; for %-formatting, <a class="reference internal" href="stdtypes.html#str.format" title="str.format"><tt class="xref py py-meth docutils literal"><span class="pre">str.format()</span></tt></a> or
<a class="reference internal" href="string.html#string.Template" title="string.Template"><tt class="xref py py-class docutils literal"><span class="pre">string.Template</span></tt></a> respectively, and
defaulting to &#8216;%&#8217; if not specified.</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">level</span></tt></td>
<td>Set the root logger level to the specified
level.</td>
</tr>
<tr class="row-even"><td><tt class="docutils literal"><span class="pre">stream</span></tt></td>
<td>Use the specified stream to initialize the
StreamHandler. Note that this argument is
incompatible with &#8216;filename&#8217; - if both are
present, a <tt class="docutils literal"><span class="pre">ValueError</span></tt> is raised.</td>
</tr>
<tr class="row-odd"><td><tt class="docutils literal"><span class="pre">handlers</span></tt></td>
<td>If specified, this should be an iterable of
already created handlers to add to the root
logger. Any handlers which don&#8217;t already
have a formatter set will be assigned the
default formatter created in this function.
Note that this argument is incompatible
with &#8216;filename&#8217; or &#8216;stream&#8217; - if both are
present, a <tt class="docutils literal"><span class="pre">ValueError</span></tt> is raised.</td>
</tr>
</tbody>
</table>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.2: </span>The <tt class="docutils literal"><span class="pre">style</span></tt> argument was added.</p>
</div>
<div class="versionchanged">
<p><span class="versionmodified">Changed in version 3.3: </span>The <tt class="docutils literal"><span class="pre">handlers</span></tt> argument was added. Additional checks were added to
catch situations where incompatible arguments are specified (e.g.
<tt class="docutils literal"><span class="pre">handlers</span></tt> together with <tt class="docutils literal"><span class="pre">stream</span></tt> or <tt class="docutils literal"><span class="pre">filename</span></tt>, or <tt class="docutils literal"><span class="pre">stream</span></tt>
together with <tt class="docutils literal"><span class="pre">filename</span></tt>).</p>
</div>
</dd></dl>

<dl class="function">
<dt id="logging.shutdown">
<tt class="descclassname">logging.</tt><tt class="descname">shutdown</tt><big>(</big><big>)</big><a class="headerlink" href="#logging.shutdown" title="Permalink to this definition">¶</a></dt>
<dd><p>Informs the logging system to perform an orderly shutdown by flushing and
closing all handlers. This should be called at application exit and no
further use of the logging system should be made after this call.</p>
</dd></dl>

<dl class="function">
<dt id="logging.setLoggerClass">
<tt class="descclassname">logging.</tt><tt class="descname">setLoggerClass</tt><big>(</big><em>klass</em><big>)</big><a class="headerlink" href="#logging.setLoggerClass" title="Permalink to this definition">¶</a></dt>
<dd><p>Tells the logging system to use the class <em>klass</em> when instantiating a logger.
The class should define <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a> such that only a name argument is
required, and the <a class="reference internal" href="../reference/datamodel.html#object.__init__" title="object.__init__"><tt class="xref py py-meth docutils literal"><span class="pre">__init__()</span></tt></a> should call <tt class="xref py py-meth docutils literal"><span class="pre">Logger.__init__()</span></tt>. This
function is typically called before any loggers are instantiated by applications
which need to use custom logger behavior.</p>
</dd></dl>

<dl class="function">
<dt id="logging.setLogRecordFactory">
<tt class="descclassname">logging.</tt><tt class="descname">setLogRecordFactory</tt><big>(</big><em>factory</em><big>)</big><a class="headerlink" href="#logging.setLogRecordFactory" title="Permalink to this definition">¶</a></dt>
<dd><p>Set a callable which is used to create a <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><tt class="xref py py-class docutils literal"><span class="pre">LogRecord</span></tt></a>.</p>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Parameters:</th><td class="field-body"><strong>factory</strong> &#8211; The factory callable to be used to instantiate a log record.</td>
</tr>
</tbody>
</table>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2: </span>This function has been provided, along with <a class="reference internal" href="#logging.getLogRecordFactory" title="logging.getLogRecordFactory"><tt class="xref py py-func docutils literal"><span class="pre">getLogRecordFactory()</span></tt></a>, to
allow developers more control over how the <a class="reference internal" href="#logging.LogRecord" title="logging.LogRecord"><tt class="xref py py-class docutils literal"><span class="pre">LogRecord</span></tt></a> representing
a logging event is constructed.</p>
</div>
<p>The factory has the following signature:</p>
<p><tt class="docutils literal"><span class="pre">factory(name,</span> <span class="pre">level,</span> <span class="pre">fn,</span> <span class="pre">lno,</span> <span class="pre">msg,</span> <span class="pre">args,</span> <span class="pre">exc_info,</span> <span class="pre">func=None,</span> <span class="pre">sinfo=None,</span> <span class="pre">**kwargs)</span></tt></p>
<blockquote>
<div><table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">name:</th><td class="field-body">The logger name.</td>
</tr>
<tr class="field-even field"><th class="field-name">level:</th><td class="field-body">The logging level (numeric).</td>
</tr>
<tr class="field-odd field"><th class="field-name">fn:</th><td class="field-body">The full pathname of the file where the logging call was made.</td>
</tr>
<tr class="field-even field"><th class="field-name">lno:</th><td class="field-body">The line number in the file where the logging call was made.</td>
</tr>
<tr class="field-odd field"><th class="field-name">msg:</th><td class="field-body">The logging message.</td>
</tr>
<tr class="field-even field"><th class="field-name">args:</th><td class="field-body">The arguments for the logging message.</td>
</tr>
<tr class="field-odd field"><th class="field-name">exc_info:</th><td class="field-body">An exception tuple, or None.</td>
</tr>
<tr class="field-even field"><th class="field-name">func:</th><td class="field-body">The name of the function or method which invoked the logging
call.</td>
</tr>
<tr class="field-odd field"><th class="field-name">sinfo:</th><td class="field-body">A stack traceback such as is provided by
<a class="reference internal" href="traceback.html#traceback.print_stack" title="traceback.print_stack"><tt class="xref py py-func docutils literal"><span class="pre">traceback.print_stack()</span></tt></a>, showing the call hierarchy.</td>
</tr>
<tr class="field-even field"><th class="field-name">kwargs:</th><td class="field-body">Additional keyword arguments.</td>
</tr>
</tbody>
</table>
</div></blockquote>
</dd></dl>

</div>
<div class="section" id="module-level-attributes">
<h2>16.6.11. Module-Level Attributes<a class="headerlink" href="#module-level-attributes" title="Permalink to this headline">¶</a></h2>
<dl class="attribute">
<dt id="logging.lastResort">
<tt class="descclassname">logging.</tt><tt class="descname">lastResort</tt><a class="headerlink" href="#logging.lastResort" title="Permalink to this definition">¶</a></dt>
<dd><p>A &#8220;handler of last resort&#8221; is available through this attribute. This
is a <a class="reference internal" href="logging.handlers.html#logging.StreamHandler" title="logging.StreamHandler"><tt class="xref py py-class docutils literal"><span class="pre">StreamHandler</span></tt></a> writing to <tt class="docutils literal"><span class="pre">sys.stderr</span></tt> with a level of
<tt class="docutils literal"><span class="pre">WARNING</span></tt>, and is used to handle logging events in the absence of any
logging configuration. The end result is to just print the message to
<tt class="docutils literal"><span class="pre">sys.stderr</span></tt>. This replaces the earlier error message saying that
&#8220;no handlers could be found for logger XYZ&#8221;. If you need the earlier
behaviour for some reason, <tt class="docutils literal"><span class="pre">lastResort</span></tt> can be set to <tt class="docutils literal"><span class="pre">None</span></tt>.</p>
<div class="versionadded">
<p><span class="versionmodified">New in version 3.2.</span></p>
</div>
</dd></dl>

</div>
<div class="section" id="integration-with-the-warnings-module">
<h2>16.6.12. Integration with the warnings module<a class="headerlink" href="#integration-with-the-warnings-module" title="Permalink to this headline">¶</a></h2>
<p>The <a class="reference internal" href="#logging.captureWarnings" title="logging.captureWarnings"><tt class="xref py py-func docutils literal"><span class="pre">captureWarnings()</span></tt></a> function can be used to integrate <a class="reference internal" href="#module-logging" title="logging: Flexible event logging system for applications."><tt class="xref py py-mod docutils literal"><span class="pre">logging</span></tt></a>
with the <a class="reference internal" href="warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><tt class="xref py py-mod docutils literal"><span class="pre">warnings</span></tt></a> module.</p>
<dl class="function">
<dt id="logging.captureWarnings">
<tt class="descclassname">logging.</tt><tt class="descname">captureWarnings</tt><big>(</big><em>capture</em><big>)</big><a class="headerlink" href="#logging.captureWarnings" title="Permalink to this definition">¶</a></dt>
<dd><p>This function is used to turn the capture of warnings by logging on and
off.</p>
<p>If <em>capture</em> is <tt class="docutils literal"><span class="pre">True</span></tt>, warnings issued by the <a class="reference internal" href="warnings.html#module-warnings" title="warnings: Issue warning messages and control their disposition."><tt class="xref py py-mod docutils literal"><span class="pre">warnings</span></tt></a> module will
be redirected to the logging system. Specifically, a warning will be
formatted using <a class="reference internal" href="warnings.html#warnings.formatwarning" title="warnings.formatwarning"><tt class="xref py py-func docutils literal"><span class="pre">warnings.formatwarning()</span></tt></a> and the resulting string
logged to a logger named <tt class="docutils literal"><span class="pre">'py.warnings'</span></tt> with a severity of <tt class="xref py py-const docutils literal"><span class="pre">WARNING</span></tt>.</p>
<p>If <em>capture</em> is <tt class="docutils literal"><span class="pre">False</span></tt>, the redirection of warnings to the logging system
will stop, and warnings will be redirected to their original destinations
(i.e. those in effect before <tt class="docutils literal"><span class="pre">captureWarnings(True)</span></tt> was called).</p>
</dd></dl>

<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<dl class="last docutils">
<dt>Module <a class="reference internal" href="logging.config.html#module-logging.config" title="logging.config: Configuration of the logging module."><tt class="xref py py-mod docutils literal"><span class="pre">logging.config</span></tt></a></dt>
<dd>Configuration API for the logging module.</dd>
<dt>Module <a class="reference internal" href="logging.handlers.html#module-logging.handlers" title="logging.handlers: Handlers for the logging module."><tt class="xref py py-mod docutils literal"><span class="pre">logging.handlers</span></tt></a></dt>
<dd>Useful handlers included with the logging module.</dd>
<dt><span class="target" id="index-1"></span><a class="pep reference external" href="http://www.python.org/dev/peps/pep-0282"><strong>PEP 282</strong></a> - A Logging System</dt>
<dd>The proposal which described this feature for inclusion in the Python standard
library.</dd>
<dt><a class="reference external" href="http://www.red-dove.com/python_logging.html">Original Python logging package</a></dt>
<dd>This is the original source for the <a class="reference internal" href="#module-logging" title="logging: Flexible event logging system for applications."><tt class="xref py py-mod docutils literal"><span class="pre">logging</span></tt></a> package.  The version of the
package available from this site is suitable for use with Python 1.5.2, 2.1.x
and 2.2.x, which do not include the <a class="reference internal" href="#module-logging" title="logging: Flexible event logging system for applications."><tt class="xref py py-mod docutils literal"><span class="pre">logging</span></tt></a> package in the standard
library.</dd>
</dl>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
  <h3><a href="../contents.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">16.6. <tt class="docutils literal"><span class="pre">logging</span></tt> &#8212; Logging facility for Python</a><ul>
<li><a class="reference internal" href="#logger-objects">16.6.1. Logger Objects</a></li>
<li><a class="reference internal" href="#logging-levels">16.6.2. Logging Levels</a></li>
<li><a class="reference internal" href="#handler-objects">16.6.3. Handler Objects</a></li>
<li><a class="reference internal" href="#formatter-objects">16.6.4. Formatter Objects</a></li>
<li><a class="reference internal" href="#filter-objects">16.6.5. Filter Objects</a></li>
<li><a class="reference internal" href="#logrecord-objects">16.6.6. LogRecord Objects</a></li>
<li><a class="reference internal" href="#logrecord-attributes">16.6.7. LogRecord attributes</a></li>
<li><a class="reference internal" href="#loggeradapter-objects">16.6.8. LoggerAdapter Objects</a></li>
<li><a class="reference internal" href="#thread-safety">16.6.9. Thread Safety</a></li>
<li><a class="reference internal" href="#module-level-functions">16.6.10. Module-Level Functions</a></li>
<li><a class="reference internal" href="#module-level-attributes">16.6.11. Module-Level Attributes</a></li>
<li><a class="reference internal" href="#integration-with-the-warnings-module">16.6.12. Integration with the warnings module</a></li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="getopt.html"
                        title="previous chapter">16.5. <tt class="docutils literal"><span class="pre">getopt</span></tt> &#8212; C-style parser for command line options</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="logging.config.html"
                        title="next chapter">16.7. <tt class="docutils literal"><span class="pre">logging.config</span></tt> &#8212; Logging configuration</a></p>
<h3>This Page</h3>
<ul class="this-page-menu">
  <li><a href="../bugs.html">Report a Bug</a></li>
  <li><a href="../_sources/library/logging.txt"
         rel="nofollow">Show Source</a></li>
</ul>

<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="logging.config.html" title="16.7. logging.config — Logging configuration"
             >next</a> |</li>
        <li class="right" >
          <a href="getopt.html" title="16.5. getopt — C-style parser for command line options"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &raquo;</li>
        <li>
          <span class="version_switcher_placeholder">3.4.3</span>
          <a href="../index.html">Documentation</a> &raquo;
        </li>

          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
          <li><a href="allos.html" >16. Generic Operating System Services</a> &raquo;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2015, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on May 02, 2015.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2.3.
    </div>

  </body>
</html>